#=
将path可视化
=#

push!(LOAD_PATH, "/Users/mary/workspace/")
using pimary

using Random
using Plots


"""
绘制坐标
"""
function draw_sket!(plt, Nt, vol)
    for it in Base.OneTo(Nt)
        plot!(plt, [0.0, vol], [it, it], line=:dot, label="", color=:black)
    end
    plot!(plt, [0.0, vol], [Nt+1, Nt+1], line=:dot,
    xlims=(0.0, vol), ylims=(0.0, Nt+2), label="", color=:black)
end


"""
绘制beads
"""
function draw_beads!(plt, ti, rpos, clr)
    for i in Base.OneTo(length(rpos))
        annotate!(plt, rpos[i], ti, ("$(i)", 10, :black, :center))
        #plot!(plt, Plots.partialcircle(0, 2π, 100))
    end
    scatter!(plt, rpos, repeat([ti], length(rpos)), markersize=15, label="", color=clr)
end


"""
绘制连线
"""
function draw_conns!(plt, ti, rpos, rprev, clr)
    for i in Base.OneTo(length(rpos))
        plot!(plt, [rpos[i], rprev[i]], [ti, ti-1], label="", color=clr)
    end
end


function run()
    Np = 5
    Nt = 8
    vol = 4
    fbup = WrdFb(Float64, Np, Nt)
    fbdn = WrdFb(Float64, Np, Nt)
    uppos = vol*rand(Np, 3)
    dnpos = vol*rand(Np, 3)
    offup = (rand(Nt-1, Np, 3).-0.5)*pimary._ThermWL
    offdn = (rand(Nt-1, Np, 3).-0.5)*pimary._ThermWL
    for ip in Base.OneTo(Np)
        fbup[ip][1, :] = uppos[ip, :]
        fbdn[ip][1, :] = dnpos[ip, :]
        for it in 2:1:Nt
            fbup[ip][it, :] = uppos[ip, :] + offup[it-1, ip, :]
            fbdn[ip][it, :] = dnpos[ip, :] + offdn[it-1, ip, :]
        end
    end
    plt = plot()
    draw_sket!(plt, Nt, vol)
    for ti in Base.OneTo(Nt)
        xpos = beads(fbup, ti)[:, 1]
        draw_beads!(plt, ti, xpos, :red)
        if ti > 1
            draw_conns!(plt, ti, xpos, beads(fbup, ti-1)[:, 1], :red)
        end
    end
    xprev = beads(fbup, Nt)[:, 1]
    sign_permute!(fbup)
    xpos = beads(fbup, 1)[:, 1]
    draw_beads!(plt, Nt+1, xpos, :red)
    draw_conns!(plt, Nt+1, xpos, xprev, :red)
    savefig("test.png")
end


run()

